package com.ocl.www.controller;

import com.ocl.www.utils.KafkaProducer;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/17.
 */
@RestController
@RequestMapping(value = "/kfk")
public class KafkaController {

    private Logger logger = Logger.getLogger(KafkaController.class);

    @RequestMapping(value = "/sendMsg",method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,String> sendMsg(@RequestBody Map<String,String> map){

        String senderId = map.get("senderId");
        String reciverId = map.get("reciverId");
        String subUserId = SecurityUtils.getSubject().getPrincipal().toString();
        if (senderId.equals(reciverId)){
            map.put("status","e");
            map.put("info","您不能和你自己聊天！");
            return map;
        }
        try{
            if (!subUserId.isEmpty()&&!subUserId.equals(reciverId)&&subUserId.equals(senderId)){

                logger.info(senderId);
                KafkaProducer kp = new KafkaProducer();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("senderId",senderId);
                jsonObject.put("reciverId",reciverId);
                jsonObject.put("createTime",new Timestamp(new Date().getTime()).toString());
                jsonObject.put("content",map.get("content"));
                kp.produce(jsonObject.toString());

                map.put("status","s");
                map.put("info","发送成功");
            }else{
                map.put("status","e");
                map.put("info","您还未登录！");
            }
        }catch(Exception e){
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/sendToRobot",method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map<String,String> sendToRobot(@RequestBody Map<String,String> map){

        String senderId = map.get("senderId");
        String reciverId = map.get("reciverId");
        String subUserId = SecurityUtils.getSubject().getPrincipal().toString();
        if (senderId.equals(reciverId)){
            map.put("status","e");
            map.put("info","您不能和你自己聊天！");
            return map;
        }
        try{
            if (!subUserId.isEmpty() &&!subUserId.equals(reciverId)&&subUserId.equals(senderId)){

                logger.info(senderId);
                KafkaProducer kp = new KafkaProducer();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("senderId",senderId);
                jsonObject.put("reciverId",reciverId);
                jsonObject.put("createTime",new Timestamp(new Date().getTime()).toString());
                jsonObject.put("content",map.get("content"));
                kp.produce(jsonObject.toString());
                logger.info("什么都没有，测试一下，无聊。。。");
//                jsonObject.put("senderId",reciverId);
//                jsonObject.put("reciverId",senderId);
//                jsonObject.put("createTime",new Timestamp(new Date().getTime()+1000).toString());
//                jsonObject.put("content","你好，我是小小狼，主人资金有限，所以暂时关闭了“登录-注册”的服务以提高服务器运转速度，现在临时派我来应付你，要是你可以帮助主人提高服务器配置，小小狼替主人谢谢您了嘿！");
//                kp.produce(jsonObject.toString());
                map.put("status","s");
                map.put("info","发送成功");
            }else{
                map.put("status","e");
                map.put("info","您还未登录！");
            }
        }catch(Exception e){
            map.put("status","e");
            map.put("info",e.getMessage());
        }
        return map;
    }
}
